window.onload = function() {
    var vm = new Vue({
      el: "#view",
      data: {
        maskLayer: 0,//营业执照大图显示隐藏
        inforImg:0,//从业信息卡
        brokerId:"",
        keyword:"",//搜索
        brokerInfoList:{},
        brokerPhoneNum :0,
        brokerRecommendHouse:[],
        countVisitsList:[],
        houseCollectionNum:[],
        query: {
          brokerId: "",
          new: 0,
          area: 3,
          price: 3,
          hot: 3,
          page: 1,
          size: 5
        },
        allHouseIds:[],
        allHouseCountVisits:[],//所有房屋浏览量列表
        more:true,
        totalNum:0,
        allHouseList:[]  
      },
      created: function() {
        this.brokerId  = this.getParams("id");
        this.query.brokerId = this.brokerId;
        this.getBrokerInfo();
        this.getPhoneStatsNum();
        this.getBrokerRecommendHouse();
        this.getBrokerAllHouse(1);
      },
      methods: {
        // 基本信息
        getBrokerInfo(){
            var $this = this;
            house_server.getBrokerInfo({ brokerId: this.brokerId }, function(data) {
                $this.brokerInfoList  = data.data;
            });
        },
        // 本月咨询
        getPhoneStatsNum(){
            var $this = this;
            house_server.getPhoneStatsNum({ resourceId: this.companyId,type:2 }, function(data) {
                $this.brokerPhoneNum  = data.data.num;
            });
        },
        // 推荐房源
        getBrokerRecommendHouse(){
            let ids=[];
            var $this = this;
            house_server.getBrokerRecommendHouse({ brokerId: this.brokerId }, function(data) {
                $this.brokerRecommendHouse   = data.data;
                for(let i =0;i<data.data.length;i++){
                    ids.push(data.data[i].buildingId);
                }
                $this.countVisitsList = $this.getCountVisits(ids);
            });
        },
        //推荐房源浏览量
        getCountVisits(list){
            var $this = this;
            let visitsList = [];
            house_server.getCountVisits({ ids: list,type: 3 }, function(data) {
                for(let i =0;i<data.data.length;i++){
                    visitsList.push(data.data[i].count);
                }
            });
            return visitsList;
        },       
        loadMore(){
          this.query.page++;
          this.getBrokerAllHouse(1);
          if(this.query.page>=(this.totalNum / this.query.size)){
            this.more = false ;
          }
        },
         //获取所有房屋详情
         getBrokerAllHouse(more){
            var $this = this;
            this.allHouseList = more ? this.allHouseList : [];
            this.allHouseIds = more ? this.allHouseIds : [];
            let list = more ? this.allHouseIds : [];
            house_server.getBrokerAllHouse(this.query, function(data) {
                if(data.data.rows.length>0){
                  data.data.rows.forEach(function (item) {
                    $this.allHouseList.push(item);
                    $this.allHouseIds.push(item.houseId);
                  });
                  $this.allHouseCountVisits = $this.getCountVisits($this.allHouseIds);
                  $this.getHouseCollectionNums($this.allHouseIds);
                }
                if($this.query.page ==1){
                  $this.more = data.data.total>$this.query.size;
                  $this.totalNum = data.data.total;
                }
            });
        }, 
        // 所有房屋收藏量列表
        getHouseCollectionNums(ids){
            var $this = this;
            house_server.getHouseCollectionNums({ houseIds: ids}, function(data) {
              $this.houseCollectionNum = data.data;
            });
        },  
        //排序--
        //最新
        sortNew(event){
              let num = event.target.getAttribute('data-time');
              this.query.new = num;
              this.query.page = 1;
              this.getBrokerAllHouse();
          },
          //热度
          sortByHot(event){
              let num = event.target.getAttribute('data-time');
              this.query.hot = num;
              this.query.page = 1;
              this.getBrokerAllHouse();
          },
          //面积
          sortByAera(event){
              let num = event.target.getAttribute('data-time');
              this.query.area = num;
              this.query.page = 1;
              this.getBrokerAllHouse();
          },
          //单价
          sortByPrice(event){
              let num = event.target.getAttribute('data-time');
              this.query.price = num;
              this.query.page = 1;
              this.getBrokerAllHouse();
          },
        //搜索
        goToEntry() {
          window.location.href = './../search/entry.html?type=3';
        },
        goDetails(item){
          window.open( "/three/household.html?name="+item.building3dCode+"&cnt=0&houseid="+item.houseId+"&buildingid="+item.buildingId);
        },
         //增加电话咨询点击次数
         addPhoneNum(){
            house_server.addPhoneNum({ resourceId: this.companyId,type: 2 }, function(res) {
                console.log(res)
            });
        },
        goHome(){
          window.location.href = "/home/html/";
        },
        // 转化时间
      forDate: function(time) {
        var timestamp = (new Date(time.replace(/-/g, "/")) * 1) / 1000;
        function zeroize(num) {
          return (String(num).length == 1 ? "0" : "") + num;
        }
        var curTimestamp = parseInt(new Date().getTime() / 1000); //当前时间戳
        var timestampDiff = curTimestamp - timestamp; // 参数时间戳与当前时间戳相差秒数
        var curDate = new Date(curTimestamp * 1000); // 当前时间日期对象
        var tmDate = new Date(timestamp * 1000); // 参数时间戳转换成的日期对象

        var Y = tmDate.getFullYear(),
          m = tmDate.getMonth() + 1,
          d = tmDate.getDate();
        var H = tmDate.getHours(),
          i = tmDate.getMinutes(),
          s = tmDate.getSeconds();
        if (timestampDiff < 60) {
          // 一分钟以内
          return "刚刚";
        } else if (timestampDiff < 3600) {
          // 一小时前之内
          return Math.floor(timestampDiff / 60) + "分钟前";
        } else if (
          curDate.getFullYear() == Y &&
          curDate.getMonth() + 1 == m &&
          curDate.getDate() == d
        ) {
          return "今天" + zeroize(H) + ":" + zeroize(i);
        } else {
          var newDate = new Date((curTimestamp - 86400) * 1000); // 参数中的时间戳加一天转换成的日期对象
          if (
            newDate.getFullYear() == Y &&
            newDate.getMonth() + 1 == m &&
            newDate.getDate() == d
          ) {
            return "昨天" + zeroize(H) + ":" + zeroize(i);
          } else if (curDate.getFullYear() == Y) {
            return (
              zeroize(m) +
              "月" +
              zeroize(d) +
              "日 " +
              zeroize(H) +
              ":" +
              zeroize(i)
            );
          } else if (timestampDiff < 259200000) {
            return Math.floor(timestampDiff / 86400000) + "天前";
          } else {
            return (
              Y +
              "年" +
              zeroize(m) +
              "月" +
              zeroize(d) +
              "日 " +
              zeroize(H) +
              ":" +
              zeroize(i)
            );
          }
        }
      },
        //图片解析
        getImgUrl: function() {
          var ip = window.location.host,
            url = "";
          if (ip.indexOf("127.0.0.1") >= 0 || ip.indexOf("localhost") >= 0) {
            url = "http://114.113.68.14:9000/image/";
          } else if (ip.indexOf("www.") >= 0) {
            url = "http://image.3dzhixuan.com/image/";
          } else {
            url = "http://" + ip + "/image/";
          }
          return url;
        },
        // 获取参数
        getParams(name) {
          var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
          var r = window.location.search.substr(1).match(reg);
          if (r != null) return unescape(r[2]);
          return null;
        }
      }
    });
  };
  